package view
{
	import com.bit101.components.ImageChangeBtn;
	import com.bit101.components.ImageRadioBtn;
	import com.bit101.components.RadioButton;
	import com.bit101.components.Text;
	import com.liu.load.LoadInfo;
	import com.liu.load.LoadManager;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import models.ModelLocator;
	
	public class SizePanle extends Sprite
	{
		private var style:StyleSheet;
		private var styleAry:Array;
		private var table:TextField = new TextField;
		private var shape:Shape;
		private var colorAry:Array = [0x666666,0xeaf4fe,0xf7dbd8];
		public var url:String;
		
		public var gW:int = 380;
		[Embed(source="assets/tab/radioBtn.png")]
		private var cls:Class;
		[Embed(source="assets/tab/radioBg.png")]
		private var cls2:Class;
		public function SizePanle(url:String)
		{
			this.x = 627;
			this.y = 580;
			this.url = url;
			var loadinfo:LoadInfo = new LoadInfo(url,LoadInfo.XML,sizeConfig);
			LoadManager.getInstance().addSingleLoad(loadinfo);
		}
		private var str:String;
		private function sizeConfig(xml:XML):void{
			this.str = xml.tab.@title;
			_source.push(str);
			for each(var myxml:XML in xml.tab.type){
				var obj:Object = new Object;
				obj.name = String(myxml.@name);
				var ary:Array = new Array;
				obj.data = ary;
				for each(var prop:XML in myxml.item){
					var o:Object = new Object;
					o.size = String(prop.@size);
					o.id = String(prop.@id);
					ary.push(o)
				}
				_source.push(obj);
			}
			init();
		}
		private function init():void{
			shape = new Shape;
			this.addChild(shape);
			
			var txt:TextField = new TextField;
			txt.width = 250;
			txt.height = 30;
			txt.selectable = false;
			txt.htmlText = "<font color='#666666' size='14'>选择尺码(单位：厘米cm)</font>";
			this.addChild(txt);
			
			var txtformat:TextFormat = new TextFormat();
			txtformat.leading = 18;
			
			table.width = 300;
			table.height = 234;
			table.x = 80;
			table.y = 24;
			table.selectable = false;
			table.multiline = true;
			table.mouseEnabled = false;
			//table.border = true;
			table.defaultTextFormat = txtformat;
			this.addChild(table);
			//this.source = new Array;
			setStyle();
			
			var str:String;
			var ary:Array;
			var htmlStr:String = '';
			var lineY:int = 58;
			var baseY:int = 20;
			for(var i:int;i<_source.length;i++){
				if(i==0){
					str = _source[0];
					ary = str.split(",");
					htmlStr = processStr(htmlStr,ary,"#ffffff");
					graphics.beginFill(colorAry[i]);
					graphics.drawRect(0,baseY,gW,30);
					baseY += 30;
				}else{
					var h:int=0;
					for(var j:int=0;j<_source[i].data.length;j++){
						str = _source[i].data[j].size;
						ary = str.split(",");
						htmlStr = processStr(htmlStr,ary,"#666666");
						var radio:ImageRadioBtn = new ImageRadioBtn(this,50,lineY);
						radio.setEmbedBitmap(cls2,cls);
						radio.addEventListener(MouseEvent.CLICK,onClick);
						radio.data = _source[i].data[j];
						radio.groupName = "size";
						lineY += 34;
						h += 34;
						drawDashed(new Point(50,lineY-10),new Point(gW,lineY-10),2,2);
					}
					var nameTxt:TextField = new TextField;
					nameTxt.width = 50;
					nameTxt.height = 30;
					nameTxt.selectable = false;
					nameTxt.htmlText = "<font color='#666666' size='14'>" + _source[i].name + "</font>";
					this.addChild(nameTxt);
					nameTxt.y = baseY + h/2;
					nameTxt.x = 5;
					
					graphics.beginFill(colorAry[i]);
					graphics.drawRect(0,baseY,gW,h);
					baseY += h;
				}
			}
			drawDashed(new Point(40,30),new Point(40,baseY),2,2);
			table.htmlText = htmlStr;
		}
		private function processStr(str:String,ary:Array,color:String):String{
			ary.reverse();
			for(var i:int=0;i<ary.length-1;i++){
				str += "<p class='" + i + "'><font color='" + color + "' size='14'>"+ ary[i] +"</font></p>"
			}
			str += "<p><font color='" + color + "' size='14'>"+ ary[i] +"</font></p>"
			return str;
		}
		private var baseY:int = 25;
		private var _source:Array = new Array;
		private var keyAry:Array = new Array;
		private function setStyle():void{
			style = new StyleSheet;
			styleAry = new Array;
			var str:String = _source[0];
			var ary:Array = str.split(',');
			var space:Number = 320/ary.length;
			
			for(var i:int = ary.length;i>=0;i--){
				var obj:Object = new Object;
				obj.leading = -100;
				obj.display = "block";
				obj.marginLeft = (ary.length - i - 1) * space;
				obj.marginRight = 320 - space - obj.marginLeft;
				obj.textAlign = "center";
				var key:String = "." + (i);
				style.setStyle(key,obj);
			}
			
			table.styleSheet = style;
			
		}
		public function drawDashed(p1:Point,p2:Point,length:Number=5,gap:Number=5):void  
		{   
			var max:Number = Point.distance(p1,p2);   
			var l:Number = 0;   
			var p3:Point;   
			var p4:Point;   
			shape.graphics.lineStyle(1,0x666666);
			while(l<max)   
			{   
				p3 = Point.interpolate(p2,p1,l/max);   
				l+=length;   
				if(l>max)l=max   
				p4 = Point.interpolate(p2,p1,l/max);   
				shape.graphics.moveTo(p3.x,p3.y)   
				shape.graphics.lineTo(p4.x,p4.y)   
				l+=gap;   
			}   
		} 
		public function onClick(event:MouseEvent):void{
			var data:Object = event.target.data;
			ModelLocator.getInstance().sizeid = data.id;
			//trace(ModelLocator.getInstance().sizeid);
		}
	}
}